<?php

namespace app\admin\model;

use think\Loader;
use think\Model;

class user extends Model
{
    protected $pk     = 'id';
    protected $table  = 'mzzs_user';
//    protected $auto   = [ 'admin_id' ];
    protected $insert = [ 'create_time' ];
//  返回原有数据  不自动进行时间转换
    public function getCreateTimeAttr($time)
    {
        return $time;
    }

    public function getAll(){
//        $data=db( 'user u' )->join('auth_group_access agc on u.id=agc.uid')->order('u.id desc')->field('u.id,u.username,u.email,u.create_time,u.description,u.is_lock,agc.group_id')->paginate( 20 );
        $data=db( 'user' )->order('id desc')->field('id,username,email,create_time,description,is_lock')->paginate( 20 );
        $gdata=db( 'user' )->alias('u')->join('auth_group_access agc','u.id=agc.uid','LEFT')->field('u.id,u.username,u.email,u.create_time,u.description,u.is_lock,agc.group_id')->paginate( 20 );
//        p($data);die;
        $where['status']=1;
        $groupData=db('auth_group')->where($where)->column('id,title','id');
        $datalist=array();
        foreach($gdata->items() as $k=>$v ){
            $datalist[$v['id']][]=$v;
        }
        foreach($datalist as $kk=>$item){
            foreach($item as $kkk=>$vvv ){
                if($vvv['group_id']){
                    $item[0]['group_id_Arr'][]=$groupData[$vvv['group_id']];
                }else{
                    $item[0]['group_id_Arr'][]='';
                }
            }
            $datalist[$kk]=$item[0];
        }

        $temp=array();
        foreach($datalist as $dk=>$dv){
            $temp[]=$dv;
        }
        $data->fieldlist=$temp;
//        p($data->fieldlist);die;
        return $data;
    }


    public function store($data){
        //验证
        //添加数据库
        $data['create_time']=time();
        $validate = Loader::validate('user');
        //如果验证不通过
        if(!$validate->check($data)){
            return ['code'=>0,'message'=>$validate->getError()];
        }
        $data['password']=md5($data['password']);
        unset($data['password2']);
        unset($data['adminRole']);
        $group_id=isset($data['group_id'])?$data['group_id']:0;
        unset($data['group_id']);
        $uid=db( 'user' )->insertGetId($data);
        if($uid){
            $groupData['uid']=$uid;
            $groupData['group_id']=$group_id;
            $result=db('auth_group_access')->insert($groupData);
        }
        if ( $uid ) {
            //执行成功
            return [ 'code' => 1 , 'message' => '操作成功' ];
        }
        else {
            return [ 'code' => 0 , 'message' => $this->getError() ];
        }
    }


    /**
     * 修改密码
     */
    public function editpass($data){
        $where['id']=session('id');
        $oldData=$this->where($where)->find();

        if(md5($data['oldpassword'])!=$oldData['password']){
            $this->error='原来的密码错误';
            return false;
        }

        $re=$this->where($where)->update(['password'=>md5($data['newpassword'])]);

        if(!$re){
            $this->error='修改密码失败 ';
            return false;
        }

        return true;




    }














}
